// initSession_ID();// 初始化session_id
var PAGE_INDEX = 1;
var preOrder_params = {}; //预生成订单参数值
var isUseCoupon = false; //使用还是查看优惠券;
$(document).ready(function () {
    var isUsed = window.location.search.indexOf("isCouponSelect");
    console.log(isUsed)
    if(isUsed >= 0) {
        $("title").text("选择优惠券");
        isUseCoupon = true;
        var arg = window.location.search;
        var str = arg.match(/isCouponSelect=(\S*)/)[1];
        preOrder_params = JSON.parse(decodeURIComponent((str)));
        console.log("preOrder_params",preOrder_params);
        var skuCouponPath = "order/user/sku_coupons";
        var skuCouponParams = {
            "STORE_ID":STORE_ID,
            "SKU_ID":preOrder_params.SKU_ID
        };
        request(skuCouponPath,skuCouponParams,loadCouponList);
    } else {
        //优惠券类型：1.满减 2.直减 3.折扣
        var path = "coupon/list";
        var params = {
            "SESSION_ID": SESSION_ID,
            "COUPON_STATE": 1,
            "PAGE_CAPACITY": 50,
            "PAGE_INDEX": PAGE_INDEX
        };
        request(path, params, loadCouponList);
    }
});
function loadCouponList(res) {
    var couponHtml = '<div class="coupon-item" coupon-type="%s" coupon-no="%s" privilege="%s">\
                           <img class="coupon-img" src="%s" alt="图片加载错误">\
                           <div class="coupon-categ">\
                               <div class="coupon-title">%s</div>\
                               <div class="coupon-date">有效期至:%s</div>\
                           </div>\
                           <div class="coupon-price"><p class="privilige">%s</p><span class="coupon-rmb-lab">%s</span></div>\
                       </div>';
    // if(PAGE_INDEX == 1 && res.BODY.COUNT != 0) $("#coupon_content").empty();
    if(PAGE_INDEX == 1 && res.BODY.COUNT != 0) {
        if(IS_BIND == "0") $(".register-prompt").css("display","block");
        $(".none-coupon-display").css("display","none");
        $("#coupon_content").empty();
    } else {
        if(IS_BIND == "1") {
            $(".to-register").css("display","none");
            $(".tobuy-coffee").css({"width":"100%","padding":"0px"});
        }
        $(".none-coupon-display").css("display","block");
    }
_.each(res.BODY.LIST,function (item, index) {
    var couponType = item.COUPON_TYPE; //
    var coupon_no = isUseCoupon ? item.COUPON_ID : item.COUPON_NO;
    // var store_type = item.STORE_TYPE;
    var end_time = item.DEADLINE ? item.DEADLINE : "：有效期内";
    var top_price = item.TOP_PRICE;
    var privilige = (isUseCoupon ? item.COUPON_FEE : item.PRIVILEGE) || 0;
    var unit = "元"
    var name = "";  //票券名
    var picSrc = "../assets/img/img_friend.png";
    if(isUseCoupon) {
        name = item.NAME;  //票券名
        if(name.indexOf("折") > -1) {
            name += "折" ;
            privilige *= 10;
            unit = "折";
            picSrc = "../assets/img/img_summer.png";
            couponType = 3;
        } else if(name.indexOf("满") > -1) {
            privilige = privilige.toFixed(2);
            picSrc = "../assets/img/img_birthday.png";
            couponType = 1;
        } else {
            privilige = privilige.toFixed(2);
            couponType = 2;
        }
    } else if(item.COUPON_TYPE == 1) {
        name = "满" + top_price + "减" + privilige;
        privilige = privilige.toFixed(2);
        picSrc = "../assets/img/img_birthday.png";
    } else if(item.COUPON_TYPE == 2) {
        name = "直减" + privilige;
        privilige = privilige.toFixed(2);
    } else if(item.COUPON_TYPE == 3) {
        privilige *= 10;
        name = privilige + "折券";
        unit = "折";
        picSrc = "../assets/img/img_summer.png";
    }
    $("#coupon_content").append(sprintf(couponHtml,couponType,coupon_no,privilige,picSrc,name,end_time,privilige,unit));
});
};
// 选择使用优惠券
$("#coupon_content").on("tap",".coupon-item",function () {
    var couponType = Number($(this).attr("coupon-type"));
    var couponNo = $(this).attr("coupon-no");
    var privilege = $(this).attr("privilege");
    preOrder_params.COUPON_TYPE = couponType;
    preOrder_params.COUPON_NO = couponNo;
    preOrder_params.PRIVILEGE = privilege;
    var params = encodeURIComponent(JSON.stringify(preOrder_params));
    if(isUseCoupon) {
        location.replace("./order_detail.html?isShare&params=" + params);
    } else {
        // location.replace("./index.html?coupon_params=" + params);

    }
});
$("#coupon_check_rule").on("tap",function () {
    location.href = "./rule.html";
});
$("#buy_coffee").on("tap",function () {
    location.replace("./index.html");
});
$("#to_register").on("tap",function () {
    location.replace("./register.html");
});
$("#toRegister").on("tap",function () {
    location.replace("./register.html");
});
